clearvars
clc

% Load results in baseline calibration

load('Results1.mat')

% Construct employment in levels in the counterfactual economy

LDCfa = LHCfa .* LDBas;
LLBas = lLBas;
LLBas(:,2:end,2:end) = repmat(lLBas(:,2:end,1),1,1,T-1).*cumprod(LDBas,3);
LLCfa = lLCfa;
LLCfa(:,2:end,2:end) = repmat(lLCfa(:,2:end,1),1,1,T-1).*cumprod(LDCfa,3);

% Labor supply shares for each region in 2001 (when welf change calculated)

lShaReg            = lLBas(:,:,2)./repmat(sum(lLBas(:,:,2),2),1,Splus);
AggWelf            = sum(WELF .* lShaReg,2)*100;
AggWelfU           = AggWelf(1:M);
SecStateWelf       = WELF(1:50,:);

% Import Bartik exposure

bartikjose         = xlsread('Inputs/InputData.xlsx','EXP','B2:B51');
barr               = bartikjose*2.63/mean(bartikjose);

% Compute baseline things

TotalLaborSupplyB  = squeeze(sum(lLBas(1:M,2:end,:),2));
TotalEmploymentB   = squeeze(sum(LLBas(1:M,2:end,:),2));
TotalLaborSupplyC  = squeeze(sum(lLCfa(1:M,2:end,:),2));
TotalEmploymentC   = squeeze(sum(LLCfa(1:M,2:end,:),2));

% Total US employment and labor supply

USEmpC = sum(TotalEmploymentC);
USEmpB = sum(TotalEmploymentB);
USLSuC = sum(TotalLaborSupplyC);
USLSuB = sum(TotalLaborSupplyB);
USUneC = (1-USEmpC./USLSuC)*100;
USUneB = (1-USEmpB./USLSuB)*100;
year   = linspace(2000,2000+T-1,T);

% Plot Figure 3 

figure(3)
plot(year,USUneC-USUneB)
ylabel('\fontsize{12} Aggregate U.S. unemployment from the shock, in %')
xlabel('\fontsize{12} Year')
axis([2000 2016 0 1.4])
xticks([2000 2004 2008 2012 2016])

% Plot Figure 4

figure(4)
scatter(barr,AggWelfU,[],'red')
hlines = fliplr(lsline);
set(hlines,'Color','red')
yline(0)
ylabel('\fontsize{12} Welfare change, in percent')
xlabel('\fontsize{12} Exposure to China')
a     = strings(50,1); 
b     = cellstr(a);
b(1 ) = {'AL'};
b(2 ) = {'AK'};
b(5 ) = {'CA'};
b(14) = {'IN'};
b(18) = {'LA'};
b(28) = {'NV'};
b(29) = {'NH'};
b(33) = {'NC'};
b(35) = {'OH'};
b(39) = {'RI'};
b(40) = {'SC'};
b(47) = {'WA'};
dx    = 0.08; 
dy    = 0.0026;
text(barr+dx,AggWelfU+dy,b,'Color','blue');

% Plot Figure 5

figure(5)
histogram(SecStateWelf(:)*100,20)
ylabel('\fontsize{12} Number of sector-states')
xlabel('\fontsize{12} Welfare change, in percent')

%% Load results in longer-shock calibration for Figure 7

load('Results2.mat')

% Construct employment in levels in the counterfactual economy

LDCfa                = LHCfa .* LDBas;
LLBas                = lLBas;
LLBas(:,2:end,2:end) = repmat(lLBas(:,2:end,1),1,1,T-1).*cumprod(LDBas,3);
LLCfa                = lLCfa;
LLCfa(:,2:end,2:end) = repmat(lLCfa(:,2:end,1),1,1,T-1).*cumprod(LDCfa,3);

% Compute employment and labor supply in baseline and counterfactual

TotalLaborSupplyB    = squeeze(sum(lLBas(1:M,2:end,:),2));
TotalEmploymentB     = squeeze(sum(LLBas(1:M,2:end,:),2));
TotalLaborSupplyC    = squeeze(sum(lLCfa(1:M,2:end,:),2));
TotalEmploymentC     = squeeze(sum(LLCfa(1:M,2:end,:),2));

% Total US employment and labor supply in baseline and counterfactual

USEmpC               = sum(TotalEmploymentC);
USEmpB               = sum(TotalEmploymentB);
USLSuC               = sum(TotalLaborSupplyC);
USLSuB               = sum(TotalLaborSupplyB);
USUneC               = (1-USEmpC./USLSuC)*100;
USUneB               = (1-USEmpB./USLSuB)*100;
year                 = linspace(2000,2000+T-1,T);

% Plot Figure 7

figure(7)
plot(year,USUneC-USUneB)
ylabel('\fontsize{12} Aggregate U.S. unemployment from the shock, in %')
xlabel('\fontsize{12} Year')
axis([2000 2028 0 2])
xticks([2000 2004 2008 2012 2016 2020 2024 2028])
